<?php
namespace app\api\controller\login;

use app\api\controller\Member;
use app\common\model\CourseCommentModel;
use app\common\model\CourseModel;
use app\common\model\UserModel;
use think\db;

class User extends Member
{
    /**
     * 获取用户信息
     */
    public function getUserInfo(){
        $user = $this->getMember();
        $this->success('获取用户信息成功',$user);
    }

    /**
     * 更新学校
     */
    public function updateSchool(){
        $user = $this->getMember();
        $school_id = $this->request->post('school_id');
        $school_name = $this->request->post('school_name');
        $new = UserModel::where('id',$user['id'])->update([
            'school_id'=>$school_id,
            'school_name'=>$school_name
        ]);
        $this->success('更新用户学校成功',$new);
    }

    /**
     * 更新角色
     */
    public function updateRole(){
        $user = $this->getMember();
        $role = $this->request->post('role');
        $new = UserModel::where('id',$user['id'])->update([
            'role'=>$role
        ]);
        $this->success('更新用户角色成功',$new);
    }

    /**
     * 更新用户信息
     */
    public function updateUser(){
        $user = $this->getMember();
        $realname = $this->request->post('realname');
        $student_no = $this->request->post('student_no');
        $teacher_no = $this->request->post('teacher_no');
        $grade_id = $this->request->post('grade_id');
        $class_id = $this->request->post('class_id');
        $new = UserModel::where('id',$user['id'])->update([
            'realname'=>$realname,
            'student_no'=>$student_no,
            'teacher_no'=>$teacher_no,
            'grade_id'=>$grade_id,
            'class_id'=>$class_id
        ]);
        $this->success('更新用户信息成功',$new);
    }

    /**
     * 提交评价
     */
    public function addComment(){
        $user = $this->getMember();
        $course_id = $this->request->post('course_id');
        $content = $this->request->post('content');
        $star = $this->request->post('star');
        $new = CourseCommentModel::create([
           'course_id'=>$course_id,
           'user_id'=>$user['id'],
           'content'=>$content,
           'star'=>$star,
        ]);
        $this->success('提交评价成功',$new);
    }

    public function getCommentStatus(){
        $course_id = $this->request->post('course_id');
        $Course = CourseModel::where('id',$course_id)->field('is_comment')->find();
        $this->success('success',$Course);
    }


    /**
     * 获取驿站长/合伙人收益
     */
    public function getPaylog(){
        $user = $this->getMember();
        $user_id = $user['id'];
        $result = array();
        $result['today_amount']=OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->where('create_time','>=',date('Y-m-d').' 00:00:00')
            ->where('create_time','<=',date('Y-m-d').' 23:59:59')
            ->sum('has_pay');
        $monthFirstDay = date('Y-m-01',time());
        $monthLastDay = date('Y-m-d', strtotime(date('Y-m-01', time()). ' 1 month -1 day'));
        $result['month_amount']=OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->where('create_time','>=',$monthFirstDay.' 00:00:00')
            ->where('create_time','<=',$monthLastDay.' 23:59:59')
            ->sum('has_pay');
        $result['total_amount']=OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->sum('has_pay');
        $result['pay_count']=OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->count();
        $this->success('获取驿站长/合伙人收益成功',$result);
    }

    /**
     * 驿站长/合伙人-订单
     */
    public function getPaylogOrderList(){
        $status = $this->request->get('status');
        $where = [];
        switch ($status){
            case 1: $where = ['status'=>ORDER_STATUS_PAY];break;
            case 2: $where = ['status'=>ORDER_STATUS_ZITI];break;
            case 3: $where = ['status'=>ORDER_STATUS_FINISH];break;
        }
        $user_id = $this->getMember()['id'];
        $page = $this->request->get('page', 1);
        $order_ids = OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->field('group_concat(order_id) as order_ids')->find()['order_ids'];
        $order = OrderModel::where('id','in',$order_ids)->where($where)->page($page,10)->order('id desc')->select();
        foreach ($order as &$value){
            $goods = OrderGoodsModel::where('order_id',$value['id'])->find();
            $value['goods'] = $goods;
            switch ($value['status']){
                case ORDER_STATUS_CANCEL: $value['status_desc'] = '已取消';break;
                case ORDER_STATUS_PAY: $value['status_desc'] = '待付款';break;
                case ORDER_STATUS_ZITI: $value['status_desc'] = '待自提';break;
                case ORDER_STATUS_FINISH: $value['status_desc'] = '已完成';break;
                default : $value['status_desc'] = '已取消';break;
            }
        }
        if (!empty($order)) {
            $this->success('获取驿站长/合伙人订单列表成功！', $order, 200);
        } else {
            $this->error('获取驿站长/合伙人订单列表失败！', '', 400);
        }
    }

    /**
     * 驿站长/合伙人-今日收益
     */
    public function getPaylogToday(){
        $user_id = $this->getMember()['id'];
        $page = $this->request->get('page', 1);
        $paylog['list'] = OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->where(function ($db){
                $posthouse_name = urldecode($this->request->get('posthouse_name'));
                $post_id = PosthouseModel::where('name',$posthouse_name)->value('id');
                if(!empty($post_id)){
                    $order_ids = OrderModel::where('post_id',$post_id)
                        ->field('group_concat(id) as order_ids')->find()['order_ids'];
                    $db->where('order_id','in',$order_ids);
                }
            })
            ->where('create_time','>=',date('Y-m-d').' 00:00:00')
            ->where('create_time','<=',date('Y-m-d').' 23:59:59')
            ->where('not_pay',0)
            ->page($page,10)
            ->field('order_id,has_pay,create_time,poster_id,partner_id')
            ->order('id desc')
            ->select();
        foreach ($paylog['list'] as &$value) {
            //找订单
            $value['order'] = OrderModel::where('id',$value['order_id'])->find();
            $value['goods'] = OrderGoodsModel::where('order_id',$value['order']['id'])->find();
        }
        $paylog['today_amount']= OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->where('create_time','>=',date('Y-m-d').' 00:00:00')
            ->where('create_time','<=',date('Y-m-d').' 23:59:59')
            ->sum('has_pay');
        $this->success('获取驿站长/合伙人今日收益成功！', $paylog, 200);
    }

    /**
     * 驿站长/合伙人-本月收益
     */
    public function getPaylogMonth(){
        $user_id = $this->getMember()['id'];
        $page = $this->request->get('page', 1);
        $monthFirstDay = date('Y-m-01',time());
        $monthLastDay = date('Y-m-d', strtotime(date('Y-m-01', time()). ' 1 month -1 day'));
        $paylog['list'] = OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->where(function ($db){
                $posthouse_name = urldecode($this->request->get('posthouse_name'));
                $post_id = PosthouseModel::where('name',$posthouse_name)->value('id');
                if(!empty($post_id)){
                    $order_ids = OrderModel::where('post_id',$post_id)
                        ->field('group_concat(id) as order_ids')->find()['order_ids'];
                    $db->where('order_id','in',$order_ids);
                }
            })
            ->where('create_time','>=',$monthFirstDay.' 00:00:00')
            ->where('create_time','<=',$monthLastDay.' 23:59:59')
            ->where('not_pay',0)
            ->page($page,10)
            ->field('order_id,has_pay,create_time,poster_id,partner_id')
            ->order('id desc')
            ->select();
        foreach ($paylog['list'] as &$value) {
            //找订单
            $value['order'] = OrderModel::where('id',$value['order_id'])->find();
            $value['goods'] = OrderGoodsModel::where('order_id',$value['order']['id'])->find();
        }
        $paylog['month_amount']= OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->where('create_time','>=',$monthFirstDay.' 00:00:00')
            ->where('create_time','<=',$monthLastDay.' 23:59:59')
            ->sum('has_pay');
        $this->success('获取驿站长/合伙人本月收益成功！', $paylog, 200);
    }

    /**
     * 驿站长/合伙人-总收益
     */
    public function getPaylogTotal(){
        $user_id = $this->getMember()['id'];
        $page = $this->request->get('page', 1);
        $paylog['list'] = OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->where(function ($db){
                $posthouse_name = urldecode($this->request->get('posthouse_name'));
                $post_id = PosthouseModel::where('name',$posthouse_name)->value('id');
                if(!empty($post_id)){
                    $order_ids = OrderModel::where('post_id',$post_id)
                        ->field('group_concat(id) as order_ids')->find()['order_ids'];
                    $db->where('order_id','in',$order_ids);
                }
                if(!empty($this->request->get('date_from'))&&!empty($this->request->get('date_to'))&&
                    $this->request->get('date_from')==$this->request->get('date_to')){
                    $db->where('create_time','>=',$this->request->get('date_from'));
                    $db->where('create_time','<=',$this->request->get('date_to').' 23:59:59');
                }else{
                    if(!empty($this->request->get('date_from'))){
                        $db->where('create_time','>=',$this->request->get('date_from'));
                    }
                    if(!empty($this->request->get('date_to'))){
                        $db->where('create_time','<=',$this->request->get('date_to').' 23:59:59');
                    }
                }
            })
            ->where('not_pay',0)
            ->page($page,10)
            ->field('order_id,has_pay,create_time,poster_id,partner_id')
            ->order('id desc')
            ->select();
        foreach ($paylog['list'] as &$value) {
            //找订单
            $value['order'] = OrderModel::where('id',$value['order_id'])->find();
            $value['goods'] = OrderGoodsModel::where('order_id',$value['order']['id'])->find();
        }
        $paylog['total_amount']= OrderPaylogModel::where('poster_id='.$user_id.' or partner_id='.$user_id)
            ->sum('has_pay');
        $this->success('获取驿站长/合伙人总收益成功！', $paylog, 200);
    }

    /**
     * 获取优惠券列表
     */
    public function getMyCoupon(){
        $user_id = $this->getMember()['id'];
        $page = $this->request->get('page', 1);
        $user_coupon = UserCouponModel::where('user_id',$user_id)
            ->page($page,10)->select();
        foreach ($user_coupon as $key=>&$value){
            $coupon = CouponModel::where('id',$value['coupon_id'])->find();
            $value['name'] = $coupon['name'];
            $value['full'] = $coupon['full'];
            $value['reduce'] =  $coupon['reduce'];
        }
        if($this->request->has('total_price','get') && !empty($this->request->get('total_price'))){
            foreach ($user_coupon as $k=>$v){
                if($v['full'] <= $this->request->get('total_price')){
                    $result[$k] = $v;
                }
            }
        }else{
            $result = $user_coupon;
        }
        if (!empty($result)) {
            $this->success('获取优惠券列表成功！', $result, 200);
        } else {
            $this->success('获取优惠券列表成功！', [], 200);
        }
    }


    /**
     * 获取所有驿站（无分页，无条件）
     */
    public function getPosthouse(){
        $user_id = $this->getMember()['id'];
        $post = PosthouseModel::where('status',1)
            ->where('poster_userid='.$user_id.' or partner_userid='.$user_id)
            ->field('name')
            ->order('id desc')
            ->select();
        $result[] = '全部驿站';
        foreach ($post as $v) {
            $result[] = $v['name'];
        }
        if(!empty($result)){
            $this->success('获取驿站成功！',$result,200);
        }else{
            $this->error('获取驿站失败！','',400);
        }
    }

}